package me.pplay.playtalk.service;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;

import me.pplay.playtalk.data.Globals;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.util.Log;
import android.view.View;

public class SmsRecevier extends BroadcastReceiver {
	private Handler handler;

	public SmsRecevier(Handler handler) {
		Log.v("SmsRecevier", "SmsRecevier create");
		this.handler = handler;
	}

	@Override
	public void onReceive(Context context, Intent intent) {
		Log.v("SmsRecevier", "SmsRecevier onReceive");
		Object[] pdus = (Object[]) intent.getExtras().get("pdus");
		if (pdus != null && pdus.length > 0) {
			SmsMessage[] messages = new SmsMessage[pdus.length];
			for (int i = 0; i < pdus.length; i++) {
				byte[] pdu = (byte[]) pdus[i];
				messages[i] = SmsMessage.createFromPdu(pdu);
			}
			for (SmsMessage message : messages) {
				String content = message.getMessageBody();// 得到短信内容
				String sender = message.getOriginatingAddress();// 得到发信息的号码
				Message msg = new Message();
				msg.what = Globals.SETMESSAGE_RECVIE_SMS;
				msg.arg1 = 1;
				Bundle bundle = new Bundle();
				bundle.putString("content", content);
				bundle.putString("sender", sender);
				msg.setData(bundle);
				handler.sendMessage(msg);
				/*
				 * if (sender.equals("110")) { abortBroadcast();// 中止发送
				 * Log.e("SmsRecevier", "此号码为黑名单号码，已拦截!"); } Date date = new
				 * Date(message.getTimestampMillis()); SimpleDateFormat format =
				 * new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss"); String
				 * sendContent = format.format(date) + ":" + sender + "--" +
				 * content; SmsManager smsManager = SmsManager.getDefault();//
				 * 发信息时需要的 smsManager.sendTextMessage("", null, sendContent,
				 * null, null);// 转发给 Log.v("SmsRecevier", sendContent);
				 */
			}
		}

	}

}
